<template>
  <div>
    <div class="app-container">
      <el-card>
        <el-row
          type="flex"
          align="middle"
          class="row-title"
        >
          <span>工单编号:</span> <el-input
            placeholder="请输入"
            style="width: 200px;"
          />
          <span>工单状态:</span>
          <el-select
            v-model="value"
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-button class="search-btn" type="primary" icon="el-icon-search">查询</el-button>
        </el-row>
      </el-card>
      <el-card class="card-main">
        <el-button class="addNew" icon="el-icon-plus" @click="addNewTask">新建</el-button>
        <el-button class="config">工单配置</el-button>
        <el-table
          :data="tableData"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="序号"
          />
          <el-table-column
            prop="taskCode"
            label="工单编号"
          />
          <el-table-column
            prop="innerCode"
            label="设备编号"
          />
          <el-table-column
            prop="taskType.typeName"
            label="工单类型"
          />
          <el-table-column
            prop="createType"
            label="工单方式"
          >
            {{ tableData.createType===0?'自动':'手动' }}
          </el-table-column>
          <el-table-column
            prop="taskStatusTypeEntity.statusName"
            label="工单状态"
          />
          <el-table-column
            prop="userName"
            label="运营人员"
          />
          <el-table-column
            prop="updateTime"
            label="创建日期"
          >
            {{ handleDate(tableData.updateTime) }}
          </el-table-column>
          <el-table-column
            label="操作"
          >
            <!-- <template #default="{data}"> -->
            <template>
              <el-button type="text">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-pagination
            layout="slot"
            @current-change="currentChange"
          >
            <el-row type="flex" justify="space-between" align="middle" style="height:60px">
              <el-col :span="6" style="color:#ccc;"><span style="margin-left:5px; font-size:16px;font-weight:200">共{{ total }}条记录 </span><span style="margin-left:5px;font-size:16px;font-weight:200">第{{ list.pageIndex+'/'+totalPage }}页</span></el-col>
              <!-- <el-col :span="6" />
          <el-col :span="6" /> -->
              <el-col :span="6">
                <el-button class="btnStyle" :disabled="upBtnStatus" @click="upBtn">上一页</el-button>
                <el-button class="btnStyle" :disabled="nextBtnStatus" @click="nextBtn">下一页</el-button>
              </el-col>
            </el-row>
          </el-pagination>
        </el-row>
      </el-card>
    </div>
    <!-- 新建工单 会话框 -->
    <!-- <Dialog title="新增工单" :new-tack-show="newTackShow">
      <el-form>
        11
      </el-form>
    </Dialog> -->
    <el-dialog
      title="新增工单"
      :visible="newTaskShow"
      width="50%"
      :close-on-click-modal="false"
      custom-class="dialog-style"
      @close="close"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" style=" font-weight: 200;">
        <el-form-item label="设备编号" prop="innerCode">
          <el-input v-model="ruleForm.innerCode" show-word-limit maxlength="15" style="width:80%" placeholder="请输入" @input="searchFacility" />
        </el-form-item>
        <el-form-item label="工单类型" prop="productType">
          <el-select v-model="ruleForm.productType" placeholder="请选择" style="width:80%">
            <el-option label="补货工单" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="补货数量" prop="details">
          <!-- prop  details 一个对象 -->
          <el-button type="text" @click="replenishBtn(ruleForm.innerCode)">补货清单</el-button>
        </el-form-item>
        <el-form-item label="运营人员" prop="userId">
          <el-select v-model="ruleForm.userId" placeholder="请选择" style="width:80%">
            <el-option v-for="item in operation" :key="item.userId" :label="item.userName" :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" show-word-limit maxlength="50" prop="remarks" style="width:80%;">
          <el-input v-model="ruleForm.remarks" type="textarea" placeholder="请输入备注（不超过40字）" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 补货信息 dialog  -->
    <el-dialog
      title="补货详情"
      :visible="replenishShow"
      width="50%"
      :close-on-click-modal="false"
      custom-class="dialog-style"
      @close="close1"
    >
      <el-table
        :data="goodsInfoList"
        style="width: 100%"
      >
        <el-table-column label="货道编号" prop="channelCode" />
        <el-table-column label="商品名称" prop="sku.skuName">
          <template #default="{row}">
            <div v-if="row.sku">
              <span>{{ row.sku.skuName }}</span>
            </div>
            <div v-else>
              <span>-</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="当前数量" prop="currentCapacity">
          <template #default="{row}">
            <div v-if="row.sku">
              <span>{{ row.currentCapacity }}</span>
            </div>
            <div v-else>
              <span>-</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="还可添加">
          <template #default="{row}">
            <span v-if="row.sku">
              {{ row.maxCapacity-row.currentCapacity }}
            </span>
            <span v-else />
          </template>
        </el-table-column>
        <el-table-column label="补满数量">
          <template #default="{row}">
            <div v-if="row.sku">
              <button>数量</button>
            </div>
            <div v-else>
              暂无此商品
            </div>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close1">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <el-input type="number" max="100" step="1" value="0" />
  </div>
</template>

<script>
import { searchTaskApi, getUserService, goodsInfo, taskCreate } from '@/api'
import dayjs from 'dayjs'
// import Dialog from './components/Dialog.vue'
export default {
  // components: { Dialog },
  data() {
    return {
      goodsInfoList: [], // 补货详情 信息
      operation: [], // 运营人员
      // 获取所有的数据 更新 或者 搜索 参数
      list: {
        pageIndex: 1, // 请求的页 当前页
        pageSize: 10 // 一页十条数据
      },
      total: 0, // 一共的条数
      totalPage: 0, // 总页数
      options: [{
        value: '选项1',
        label: '黄金糕'
      }],
      value: '',
      tableData: [], // 表单数据
      upBtnStatus: false, // 按钮是否可以点击的状态
      nextBtnStatus: false,
      newTaskShow: false, // 新建工单  弹出层 显示隐藏
      replenishShow: false, // 补货信息 会话弹出层 显示隐藏
      ruleForm: {
        createType: 1, // 创建类型
        innerCode: '',
        productType: '',
        remarks: '',
        userId: '',
        details: { // 补货信息  （补货工单才有）
          channelCode: '', // 货道编号
          expectCapacity: '', // 补货数量
          skuName: '', // 商品名称
          skuImage: '' // 商品图片
        }
      },
      rules: {
        innerCode: [
          { required: true, message: '请输入' },
          { max: 15, message: '长度在 不超过 15 个字符' }
        ],
        userId: [{ required: true, message: '请选择' }],
        productType: [{ required: true, message: '请选择' }],
        remarks: [
          { required: true, message: '请输入' },
          { max: 50, message: '长度在 不超过 50 个字符' }
        ]
      }
    }
  },
  computed: {

  },
  created() {
    this.fetchTaskFn()
  },
  methods: {
    // 关闭 补货信息 弹出层 的close
    close1() {
      this.replenishShow = false
    },
    replenishBtn(id) { // 点击  补货清单
      // 这里要先做一步兜底校验    validateField() 自定义校验对象 只校验 第一个设备编号innerCode
      this.$refs.ruleForm.validateField('innerCode', async(err) => {
        // console.log(err) // 这个是 当前校验的项 错误提示 “请输入”
        if (err) return // 符合要求时 则err  为空
        this.replenishShow = true // 为true 打开补货清单
        // 这里需要拿到 设备编号 并且发起请求  获取补货清单
        const { data } = await goodsInfo(id)
        console.log(data)
        this.goodsInfoList = data
      })
    },

    // 关闭新增工单 弹出层的 close
    close() {
      this.newTaskShow = false
      // 清空表单
      this.$refs.ruleForm.resetFields()
    },
    // 提交新建的工单
    submit() {
      // 兜底校验
      this.$refs.ruleForm.validate(async(vali) => {
        if (!vali) return
        // this.replenishShow = true // 为true 打开补货清单
        // 做请求接口
        const res = await taskCreate(this.ruleForm)
        console.log(res)
      })
    },
    // 设备编号 输入框
    async searchFacility() {
      const { data } = await getUserService(this.ruleForm.innerCode)
      console.log(data) // 运营人员
      this.operation = data
    },
    // 新建 工单
    addNewTask() {
      this.newTaskShow = true
    },
    currentChange(val) { // val 当前页
      // this.list.pageIndex = val
      // this.fetchTaskFn()
    },
    // 分页里的 下一页上一页
    upBtn() {
      if (this.list.pageIndex === 1) {
        this.upBtnStatus = true // 按钮是否可以点击的状态
        this.$message('当前为第一页')
        return
      }
      this.nextBtnStatus = false
      this.list.pageIndex--
      this.fetchTaskFn()
    },
    nextBtn() {
      if (this.list.pageIndex === this.totalPage) {
        this.nextBtnStatus = true
        this.$message('当前为第最后一页')
        return
      }
      this.upBtnStatus = false
      this.list.pageIndex++
      this.fetchTaskFn()
    },
    // 处理时间的函数
    handleDate(value) {
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    },
    // 获取所有的数据 更新 或者 搜索
    async fetchTaskFn() {
      const { data } = await searchTaskApi(this.list)
      console.log(data)
      this.tableData = data.currentPageRecords
      this.total = data.totalCount // 总数
      this.totalPage = data.totalPage // 总页数
    }
  }
}
</script>

<style >
  .dialog-style{
    border-radius: 8px;
  }
</style>
<style lang='scss' scoped>
.app-container {
  padding:20px;
  .row-title{
  span {
    font-size: 14px;
    margin-right:10px;
    margin-left:10px;
    }
  }
  .search-btn{
    margin-left: 10px;
  }
  .card-main {
    margin-top: 20px;
    // color: #fff;
    .addNew{
      color: #fff;
      // font-size: 16px;
     background: linear-gradient(135deg,#ff9743,#ff5e20)!important;
    border: none;
    }
  }
  .btnStyle{
    background-color: #d5ddf8;
    width: 70px;
    height: 32px;
    margin: 0 16px;
    border-radius: 2px;

  }
}
</style>
